package com.yaoxun.bored.validator;

import java.util.Set;

import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

@Component
public class ValidatorHelper {
	
	@Inject
	private Validator validator;
	
	/**
	 * 使用hibernate-validator 校验
	 * @author Loren
	 * @createTime 2018年1月31日 下午2:24:57
	 * @param object
	 * @param groups
	 * @return
	 */
	public BindingResult validate(Object object, Class<?>... groups) {
		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
		BindingResult result = new CusBindingResult(constraintViolations);
		return result;
	}
	
	/**
	 * 校验单个值
	 * @author Loren
	 * @createTime 2018年2月2日 下午5:00:10
	 * @param beanType
	 * @param propertyName
	 * @param value
	 * @param groups
	 * @return
	 */
	public <T> BindingResult validateValue(Class<T> beanType,
			  String propertyName,
			  Object value,
			  Class<?>... groups) {
		Set<ConstraintViolation<T>> constraintViolations = validator.validateValue(beanType, propertyName, value, groups);
		BindingResult result = new CusBindingResult(constraintViolations, null);
		return result;
	}
	
}
